package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.exception.CustomException;
import com.itheima.reggie.mapper.EmployeeMapper;
import com.itheima.reggie.mapper.SetMealMapper;
import com.itheima.reggie.service.EmployeeService;
import com.itheima.reggie.service.SetMealDishService;
import com.itheima.reggie.service.SetMealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
public class SetMealServiceImpl extends ServiceImpl<SetMealMapper, Setmeal> implements SetMealService {
    @Autowired
    //中间表  替代了关联菜品表 添加
    private SetMealDishService setMealDishService;
    @Override
    public void saveSetmealAndDish(SetmealDto setmealDto) {
        //添加基本信息
        save(setmealDto);
        //获取中间表集合信息 没有套餐表的id  有菜品表的id
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        //遍历中间表集合信息
        for (final SetmealDish setmealDish : setmealDishes) {
            //中间表添加套餐表的id  因为中间表没有菜品表的id 需要设置  才能关联起来
            setmealDish.setSetmealId(setmealDto.getId());
        }
        //添加多个菜品数据
        setMealDishService.saveBatch(setmealDishes);
    }

    @Override
    @Transactional //事物管理
    public void removes(List<Long> ids) {
        //先判断 ids能不能删
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        //状态是1的不可以删除    status = 1 and id in(ids)
        wrapper.eq(Setmeal::getStatus,1);
        wrapper.in(Setmeal::getId,ids);
        //根据条件查询出来的条数
        int count = count(wrapper);
        if (count>0){
            //有启用的不能删除
            throw new CustomException("不能删,有人可能在下单");
        }
        //禁用状态可以删除
            removeByIds(ids);
        //
        LambdaQueryWrapper<SetmealDish> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.in(SetmealDish::getSetmealId,ids);
        setMealDishService.remove(wrapper1);

    }

    @Override
    public SetmealDto getSetmealAndSetmealDish(Long id) {
        SetmealDto setmealDto = new SetmealDto();
        Setmeal byId = getById(id);
        BeanUtils.copyProperties(byId,setmealDto);
        //拼接sql语句
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId,id);
        List<SetmealDish> list = setMealDishService.list(wrapper);
        setmealDto.setSetmealDishes(list);
        return setmealDto;
    }

    @Override
    @Transactional //事物管理  除了查询都要加事物管理
    public void PutSetmealAndSetmealDish(SetmealDto setmealDto) {
        updateById(setmealDto);

        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId,setmealDto.getId());
        //因为有修改和添加操作,避免这个两个操作,直接删除 在添加新数据,更方便
        setMealDishService.remove(wrapper);
        //中间表对象集合
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        //判断有没有值
        if (setmealDishes!=null&&setmealDishes.size()>0){
            //取出每一个中间表对象
            for (final SetmealDish setmealDish : setmealDishes) {
                //给每个中间表加对应的套餐对象id
                //添加中间表没有套餐表的id 有菜品表的id 所以要加id
                setmealDish.setSetmealId(setmealDish.getId());
            }
        }
        setMealDishService.update(wrapper);

    }

}
